//
//  LYVideoWebViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/18.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYVideoWebViewController.h"
#import "WebViewJavascriptBridge.h"
#import "LYCommentListViewController.h"
#import "LYVideoModel.h"
#import "LYAssesmentViewController.h"
#import "LYTeacherWebViewController.h"
#import "LYOrzWebViewController.h"
#import <AFNetworking.h>
#import "LJSessionDownloader.h"

#define videoMainUrl @"http://esj.zzzedu.cn/webview/view/video/video_details.php?"

@interface LYVideoWebViewController ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (copy, nonatomic) NSString *urlStr;

@property (nonatomic, copy) NSString *ID;
@property WebViewJavascriptBridge *bridge;


@end

@implementation LYVideoWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"视频详情";
    
    self.fd_prefersNavigationBarHidden = NO;
    
    [self setUpWebView];
    [self buildBridge];
    
}


#pragma mark - 构造方法
+ (instancetype)videoWebVCWithID:(NSString *)ID{
    
    LYVideoWebViewController *webVC = [[self alloc] init];
    webVC.ID = ID;
    return webVC;
}

#pragma mark - 重写方法
- (void)setID:(NSString *)ID{
    
    _ID = ID;
    
    NSString *parameter = [NSString stringWithFormat:@"course_id=%@&token=%@",self.ID,LYToken];
    self.urlStr = [videoMainUrl stringByAppendingString:parameter];
    
}

#pragma mark - 初始化
/**
 *  初始化webView
 */
- (void)setUpWebView{
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //自适应屏幕
    self.webView.scalesPageToFit = YES;
    
    WS(weakSelf);
    
    //加载请求
    if(self.urlStr){
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
        
        [self.webView loadRequest:request];
    }
    
    
    
}

/**
 *  桥接JS
 */
- (void)buildBridge{
    
    if (_bridge) { return; }
    
    __weak typeof(self) weakSelf = self;
    
    [WebViewJavascriptBridge enableLogging];
    
    _bridge = [WebViewJavascriptBridge bridgeForWebView:_webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
        
        
    }];
    
    //跳转机构或者老师
    [_bridge registerHandler:@"gotoTeacOrOrgzHome" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        NSInteger type = [data[@"role_id"] integerValue];
        NSString *data_id = data[@"user_id"];
        
        switch (type) {
            case 1:{
                LYOrzWebViewController *orzVC = [LYOrzWebViewController orzWebVCWithID:data_id];
                [weakSelf.navigationController pushViewController:orzVC animated:YES];
                
                
            }
                break;
                
            case 2:{
                LYTeacherWebViewController *orzVC = [LYTeacherWebViewController teaWebVCWithID:data_id];
                [weakSelf.navigationController pushViewController:orzVC animated:YES];
                
            }
                break;
            default:
                break;
        }
        
    }];
    
 

    
    
}

#pragma mark - 网络请求
/**
 *  收藏
 */
- (void)collect{
    
    [MBProgressHUD showMessage:nil];
    [LYRequestTool collectionWithToken:LYToken type:ObjectTypeVideo data_id:self.ID success:^{
        
        [MBProgressHUD hideHUD];
        [MBProgressHUD showSuccess:@"收藏成功"];
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
    }];
}

#pragma mark - 按钮点击
- (IBAction)collectBtnOnclick:(id)sender {
    
    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *model) {
        [self collect];
    }];
    
    
}


- (IBAction)commentBtnOnclick:(id)sender {
    
    LYAssesmentViewController *commentVC = [[LYAssesmentViewController alloc] init];
    commentVC.hideComponent = YES;
    commentVC.ID = self.ID;
    commentVC.placeHolder = @"你觉得视频怎样";
    commentVC.type = CommentTypeVideo;
    [self.navigationController pushViewController:commentVC animated:YES];
}


- (IBAction)downLoadBtnOnclick:(id)sender {
    
    [_bridge callHandler:@"getDownLoadUrl" data:nil responseCallback:^(id response) {
        
        [MBProgressHUD showOnlyText:@"开始下载视频"];
        NSString *name = [NSString stringWithFormat:@"%d.mp4",arc4random()];
     [[LJSessionDownloader downloadWithURLString:response contentPath:[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:name] withProgress:^(CGFloat progress) {
    
         
     } completion:^{
         
         [MBProgressHUD showSuccess:@"视频已下载完毕"];
         
     }] start];
        
    }];
}

#pragma mark - webView代理

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [MBProgressHUD showMessage:nil];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [MBProgressHUD hideHUD];
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    
    [MBProgressHUD hideHUD];
    [MBProgressHUD showError:@"网络不给力" toView:webView];
}


@end
